<?php
/**
 * Created by PhpStorm.
 * User: yangfan
 * Date: 2019/11/16
 * Time: 7:11 PM
 */

namespace App\BO\Coupon\CouponProductSkuCheck;

use Illuminate\Support\Facades\Log;

/**
 * 优惠券可使用服务种类检查
 */
class ProductBTypeCheck implements ICouponProductSkuCheck
{
    public static function check(CouponProductSkuCheckMeta $checkMeta, \Closure $next)
    {
        $coupon = $checkMeta->getCouponEntity();
        $productEntity = $checkMeta->getProductEntity();

        if ($coupon->getBtype()) {
            $btypes = explode(',', $coupon->getBtype());
            if (!in_array($productEntity->getBType(), $btypes)) {
                Log::info('优惠券' . $coupon->getId() . 'Btype与当前商品的Btype不符');
                $checkResultWrapper = new CheckResultWrapper($checkMeta->getCouponEntity());
                $checkResultWrapper->setMessage('此商品不能用此优惠券');
                return $checkResultWrapper;
            }
        }
        return $next($checkMeta);
    }
}
